package com.zahb.qadx.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.zahb.qadx.util.DisplayUtil;

/**
 * Created by Zdw on 2020/11/19 18:42
 */
public class StatisticsView extends View {

    private int mJoinColor, mUnJoinColor;// 弧形色值
    private Paint mPaint, mTextPaint;// 画笔
    private String mJoinNumStr, mUnJoinNumStr;// 人数
    private int mWidth, mHeight;// 宽高
    private int mArcWidth;// 圆弧宽度
    private float mJoinSweepAngle, mUnJoinSweepAngle;// 弧度
    private RectF mArcRectF;// 圆弧矩阵
    private int mPointWidth;// 圆点宽度

    public StatisticsView(Context context) {
        this(context, null);
    }

    public StatisticsView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatisticsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mJoinColor = Color.parseColor("#5C89FF");
        mUnJoinColor = Color.parseColor("#E2E2E2");
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.BUTT);
        mJoinSweepAngle = 90f;
        mUnJoinSweepAngle = 180f - mJoinSweepAngle;

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(DisplayUtil.sp2px(7f));
        mTextPaint.clearShadowLayer();
        mJoinNumStr = "0人";
        mUnJoinNumStr = "0人";

        mPointWidth = DisplayUtil.dip2px(4f);
    }

    public void setNum(int joinNum, int unJoinNum) {
        mJoinNumStr = joinNum + "人";
        mUnJoinNumStr = unJoinNum + "人";
        if (joinNum < 0)
            joinNum = 0;
        if (unJoinNum < 0)
            unJoinNum = 0;
        if (joinNum == 0 && unJoinNum == 0) {
            mJoinSweepAngle = 90f;
            mUnJoinSweepAngle = 180f - mJoinSweepAngle;
        } else {
            mJoinSweepAngle = 180f / (joinNum + unJoinNum) * joinNum;
            mUnJoinSweepAngle = 180f - mJoinSweepAngle;
        }
        postInvalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldW, int oldH) {
        super.onSizeChanged(w, h, oldW, oldH);
        mWidth = w;
        mHeight = h;
        mArcWidth = mWidth / 5;
        mArcRectF = new RectF(mArcWidth / 2, mArcWidth / 2, mWidth - mArcWidth / 2, mHeight * 2 - mArcWidth / 2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制圆弧
        mPaint.setStrokeCap(Paint.Cap.BUTT);
        mPaint.setStrokeWidth(mArcWidth);
        if (mJoinSweepAngle > 0) {
            mPaint.setColor(mJoinColor);
            canvas.drawArc(mArcRectF, -180, mJoinSweepAngle, false, mPaint);
        }
        if (mUnJoinSweepAngle > 0) {
            mPaint.setColor(mUnJoinColor);
            canvas.drawArc(mArcRectF, -180 + mJoinSweepAngle, mUnJoinSweepAngle, false, mPaint);
        }

        float width = mTextPaint.measureText("未参加考试人数") + mPointWidth * 2f;// 圆点左侧到文本右侧的距离

        // 绘制色值说明
        Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        mTextPaint.setColor(Color.parseColor("#020101"));
        canvas.drawText("参加考试人数", mWidth / 2f - width / 2 + mPointWidth, mHeight - metrics.bottom * 2 + metrics.top, mTextPaint);
        canvas.drawText("未参加考试人数", mWidth / 2f - width / 2 + mPointWidth, mHeight - metrics.bottom, mTextPaint);

        // 绘制圆点
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(mPointWidth);
        mPaint.setColor(mJoinColor);
        canvas.drawPoint(mWidth / 2f - width / 2, mHeight - (metrics.bottom - metrics.top) / 2 * 3, mPaint);
        mPaint.setColor(mUnJoinColor);
        canvas.drawPoint(mWidth / 2f - width / 2, mHeight - (metrics.bottom - metrics.top) / 2, mPaint);

        // 绘制人数
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(Color.WHITE);
        canvas.drawText(mJoinNumStr, mArcWidth / 2f, mHeight + metrics.top, mTextPaint);
        canvas.drawText(mUnJoinNumStr, mWidth - mArcWidth / 2f, mHeight + metrics.top, mTextPaint);
    }
}
